﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class CollectService : ICollectService
    {
        /// <summary>
        /// 添加商品收藏
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="goodId">商品id</param>
        /// <returns></returns>
        public long Add(long userId, long goodId)
        {
            Collect collect = new Collect();
            collect.UserId = userId;
            collect.GoodId = goodId;
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Collect> bs = new BaseService<Collect>(ctx);
                // 判断用户要添加的商品是否已经存在
                bool exist = bs.GetAll().Any(c => c.UserId == userId && c.GoodId == goodId);
                if (exist)
                {
                    // 获取该商品，通过抛异常告诉用户该商品已经存在
                    var colle = bs.GetAll().Where(c => c.UserId == userId && c.GoodId == goodId).SingleOrDefault();
                    throw new ArgumentException("用户名为：" + colle.User.Name + "已添加了商品：" + colle.Good.Name);
                }
                ctx.Collects.Add(collect);
                ctx.SaveChanges();
                return collect.Id;
            }
        }

        public CollectDTO[] GetAll()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Collect> bs = new BaseService<Collect>(ctx);
                var collects = bs.GetAll();
                return collects.ToList().Select(c => ToDTO(c)).ToArray();
            }
        }
        /// <summary>
        /// 通过用户id获取该用户所有收藏的商品
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public CollectDTO[] GetCollectsByUserId(long userId, int pageIndex, int pageSize)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Collect> bs = new BaseService<Collect>(ctx);
                #region 原始代码
                //var collects = bs.GetAll();
                //List<long> listGoodIds = new List<long>();
                //foreach (var collect in collects)
                //{
                //    listGoodIds.Add(collect.GoodId);
                //} 
                // 将list集合转化为数组
                //return listGoodIds.ToArray();
                #endregion

                var goodIds = bs.GetAll().Where(c => c.UserId == userId)
                    .Include(c => c.User).Include(c => c.Good).AsNoTracking()
                    .OrderByDescending(c => c.CreateDateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize);

                return goodIds.ToList().Select(c => ToDTO(c)).ToArray();
            }
        }



        /// <summary>
        /// 软删除该用户收藏的商品
        /// </summary>
        /// <param name="collectId">收藏品id</param>
        public void MakeDelete(long collectId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Collect> bs = new BaseService<Collect>(ctx);
                bs.MakeDelete(collectId);
            }
        }

        public CollectDTO ToDTO(Collect coll)
        {
            CollectDTO dto = new CollectDTO();
            dto.IsExist = coll.IsDeleted == false ? "还在" : "已删";
            dto.UserName = coll.User.Name;
            dto.GoodName = coll.Good.Name;
            dto.CreateDateTime = coll.CreateDateTime;
            dto.GoodId = coll.GoodId;
            dto.Id = coll.Id;
            dto.UserId = coll.UserId;
            return dto;
        }


    }
}
